package GradeQuery;

/**
 * @Author maojiangyue
 * @Date 2025/10/18
 * @Description 成绩查询及实现类
 */
public class GradeService {
    public static void main(String[] args) {
        ClassGrades classGrades = new ClassGrades(new int[]{90, 59, 91, 88, 96});
        try {
            System.out.println("学生成绩: " + queryScore(classGrades, 3));
        } catch (IndexOutOfBoundsException e) {
            System.err.println("查询的id不存在");
        } catch (FailException e) {
            System.err.println(e.getMessage());
        }

        try {
            System.out.println("学生成绩: " + queryScore(classGrades, 2));
            System.out.println("学生成绩: " + queryScore(classGrades, 1));
        } catch (IndexOutOfBoundsException e) {
            System.err.println("查询的id不存在");
        } catch (FailException e) {
            System.err.println(e.getMessage());
        }

        try {
            System.out.println("学生成绩: " + queryScore(classGrades, 12));
        } catch (IndexOutOfBoundsException e) {
            System.err.println("查询的id不存在");
        } catch (FailException e) {
            System.err.println(e.getMessage());
        }

        try {
            System.out.println("学生成绩: " + queryScore(classGrades, 5));
        } catch (IndexOutOfBoundsException e) {
            System.err.println("查询的id不存在");
        } catch (FailException e) {
            System.err.println(e.getMessage());
        }
    }

    public static int queryScore(ClassGrades classGrades, int id)
            throws IndexOutOfBoundsException, FailException {
        int mark = classGrades.getGradeById(id);
        if (mark < 60) {
            throw new FailException("学生成绩不及格: " + mark);
        }
        return mark;
    }
}
